package crudfx.field;
import crudfx.interop.*;
import crudfx.data.*;
import javax.swing.*;
import java.awt.event.*;
/**
 * @profile desktop
 * @author Sergey Surikov
 */
public class PasswordField extends UIComponent{
    public var jPasswordField:JPasswordField;
    public var text:String="" on replace {jPasswordField.setText(text);}
    public var onLostFocus:function():Void;

    // Added key listeners
    //public var onKeyPressed:function():Void;
    //public var onKeyReleased:function():Void;
    public var onKey:function():Void;
    public var keyEvent: KeyEvent;

    init{
        jPasswordField=new JPasswordField(text);
        var focusAdapter:FocusAdapter=FocusAdapter{
            //override public function focusGained(evt:FocusEvent):Void { }
            override public function focusLost(evt:FocusEvent):Void {                
                text=new String(jPasswordField.getPassword());
                onLostFocus();
               }
            };
        jPasswordField.addFocusListener(focusAdapter);
        var keyAdapter:KeyAdapter=KeyAdapter{
            override public function keyPressed(evt:KeyEvent):Void{
                keyEvent = evt;
                var ss:Integer=jPasswordField.getSelectionStart();
                var se:Integer=jPasswordField.getSelectionEnd();
                text=new String(jPasswordField.getPassword());
                jPasswordField.setSelectionStart(ss);
                jPasswordField.setSelectionEnd(se);
                }
            override public function keyReleased(evt:KeyEvent):Void{                
                keyEvent = evt;
                var ss:Integer=jPasswordField.getSelectionStart();
                var se:Integer=jPasswordField.getSelectionEnd();
                text=new String(jPasswordField.getPassword());
                jPasswordField.setSelectionStart(ss);
                jPasswordField.setSelectionEnd(se);
                }
            override public function keyTyped(evt:KeyEvent):Void{                
                keyEvent = evt;
                var ss:Integer=jPasswordField.getSelectionStart();
                var se:Integer=jPasswordField.getSelectionEnd();
                text=new String(jPasswordField.getPassword());
                jPasswordField.setSelectionStart(ss);
                jPasswordField.setSelectionEnd(se);
                onKey();
                }
            };
        jPasswordField.addKeyListener(keyAdapter);
        }
    override public function getJComponent():JComponent{
        return jPasswordField;
        }
    override public function highlight(s:String):Boolean{
        return false;
        }
    override public function gather():DataTree{
        var me:DataTree=DataTree{ name:"PasswordField" };
        //me.node("text").asString("").value=text;
        return me;
        }
    }
